﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HumanManagement.BusinessLogic;
using HumanManagement.BusinessObjects;
using HumanManagement.DataAccess;
using System.Data;

namespace BSC_HumanManagement.Usercontrols
{
    public partial class uc_KhaNangNgoaiNgu_Detail : System.Web.UI.UserControl
    {
        int itemID = 0;
        ForeignLanguageSkill objForeignLanguageSkill = new ForeignLanguageSkill();
        ForeignLanguageSkillBL objForeignLanguageSkillBL = new ForeignLanguageSkillBL();
        //public bool permissionAddNew = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtIssuedOn.MaxDate = DateTime.MaxValue;
            this.txtIssuedOn.MinDate = DateTime.MinValue;
            PersonalProfileBL _objPersonalProfileBL = new PersonalProfileBL();
            foreach (DataRow item in _objPersonalProfileBL.GetDataSet(Convert.ToInt32(Session["PersonalProfileID"].ToString())).Tables[0].Rows)
            {
                this.ddlPersonalProfileID.Items.Add(new ListItem(item["FullName"].ToString(), item["ID"].ToString()));
            }
            ForeignLanguageBL objForeignLanguageBL = new ForeignLanguageBL();
            foreach (DataRow item in objForeignLanguageBL.GetDataSet().Tables[0].Rows)
            {
                this.ddlForeignLanguageID.Items.Add(new ListItem(item["ForeignLanguage"].ToString(), item["ID"].ToString()));
            }
            ForeignLanguageLevelBL objForeignLanguageLevelBL = new ForeignLanguageLevelBL();
            foreach (DataRow item in objForeignLanguageLevelBL.GetDataSet().Tables[0].Rows)
            {
                this.ddlForeignLanguageLevelID.Items.Add(new ListItem(item["ForeignLanguageLevel"].ToString(), item["ID"].ToString()));
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Khả năng ngoại ngữ Detail")
            {
                LoadData();
                DisableControl();
                ShowData();
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Khả năng ngoại ngữ Edit")
            {
                if (LoadData())
                {
                    ShowData();
                    this.btnDelete.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hồ sơ nhân viên mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Khả năng ngoại ngữ Delete")
            {
                if (LoadData())
                {
                    DisableControl();
                    ShowData();
                    this.btnSave.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hợp đồng mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
        }

        private void ShowData()
        {
            this.txtProvidedBy.Text = objForeignLanguageSkill.ProvidedBy;
            this.txtIssuedOn.SelectedDate = objForeignLanguageSkill.IssuedOn;
            for (int i = 0; i < this.ddlPersonalProfileID.Items.Count; i++)
            {
                if (this.ddlPersonalProfileID.Items[i].Value.Equals(objForeignLanguageSkill.PersonalProfileID.ToString()))
                {
                    this.ddlPersonalProfileID.SelectedIndex = i;
                }
            }
            for (int i = 0; i < this.ddlForeignLanguageID.Items.Count; i++)
            {
                if (this.ddlForeignLanguageID.Items[i].Value.Equals(objForeignLanguageSkill.ForeignLanguageID.ToString()))
                {
                    this.ddlForeignLanguageID.SelectedIndex = i;
                }
            }
            for (int i = 0; i < this.ddlForeignLanguageLevelID.Items.Count; i++)
            {
                if (this.ddlForeignLanguageLevelID.Items[i].Value.Equals(objForeignLanguageSkill.ForeignLanguageLevelID.ToString()))
                {
                    this.ddlForeignLanguageLevelID.SelectedIndex = i;
                }
            }
        }

        private void DisableControl()
        {
            this.txtIssuedOn.Enabled = false;
            this.txtProvidedBy.Enabled = false;
            this.ddlPersonalProfileID.Enabled = false;
            this.ddlForeignLanguageID.Enabled = false;
            this.ddlForeignLanguageLevelID.Enabled = false;
        }

        private bool LoadData()
        {
            itemID = Convert.ToInt32(Request.QueryString["ID"]);
            this.objForeignLanguageSkill = objForeignLanguageSkillBL.GetByID(itemID, Convert.ToInt32(Session["PersonalProfileID"].ToString()));
            if (objForeignLanguageSkill == null)
            {
                return false;
            }
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.objForeignLanguageSkill.ProvidedBy = txtProvidedBy.Text;
            this.objForeignLanguageSkill.ForeignLanguageID = Convert.ToInt32( ddlForeignLanguageID.SelectedValue);
            this.objForeignLanguageSkill.ForeignLanguageLevelID = Convert.ToInt32(ddlForeignLanguageLevelID.SelectedValue);
            this.objForeignLanguageSkill.PersonalProfileID = Convert.ToInt32(ddlPersonalProfileID.SelectedValue);
            this.objForeignLanguageSkill.LastDate = DateTime.Now;
            this.objForeignLanguageSkill.ForeignLanguageSkillStatus = "1";
            if (txtIssuedOn.SelectedDate.HasValue)
            {
                this.objForeignLanguageSkill.IssuedOn = (DateTime)this.txtIssuedOn.SelectedDate;
            }
            else
            {
                this.objForeignLanguageSkill.IssuedOn = DateTime.MaxValue;
            }

            if (Request.QueryString["ID"] == null)
            {
                objForeignLanguageSkillBL.Add(objForeignLanguageSkill);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Tạo mới thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Khả năng ngoại ngữ");
            }

            if (Request.QueryString["FunctionName"] == "Khả năng ngoại ngữ Edit")
            {
                objForeignLanguageSkillBL.Update(objForeignLanguageSkill);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Sửa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Khả năng ngoại ngữ");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                objForeignLanguageSkill = objForeignLanguageSkillBL.GetByID(Convert.ToInt32(Request.QueryString["ID"]), Convert.ToInt32(Session["PersonalProfileID"].ToString()));
                objForeignLanguageSkill.ForeignLanguageSkillStatus = "0";
                objForeignLanguageSkillBL.Update(objForeignLanguageSkill);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Xóa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Khả năng ngoại ngữ");
            }
            else { }
        }
    }
}